<template>
  <div>
    <v-edit :title="init?.isAdd?'移动端新增':'移动端编辑'" :init="comm" :infos="info" @reset="reset" @save="save"
            @close="close" :rules="rules">
      <template #formItem>
        <div class="two-col intro-y">
          <wde-input label="设备编码" prop="imei" v-model="info.imei"/>
        </div>
        <div class="two-col intro-y">
          <wde-input label="设备用户" prop="mobilePerson" v-model="info.mobilePerson"/>
          <wde-input label="手机号" prop="tel" v-model="info.tel"/>
        </div>
        <div class="two-col intro-y">
          <wde-input label="设备型号" prop="userName" v-model="info.model"/>
          <wde-input label="型号代码" prop="userName" v-model="info.modelCode"/>
        </div>
        <!-- <div class="two-col intro-y">
          <wde-price label="金额(万元)" v-model="info.price" />
        </div>
        <div class="two-col intro-y">
          <wde-muti-select label="等级" v-model="info.levels" :options="options" />
        </div>
        <div class="two-col intro-y">
          <wde-select label="项目类型" v-model="info.type" :options="options" />
          <wde-muti-select label="等级" v-model="info.levels" :options="options" />
        </div>
        <div class="two-col intro-y">
          <wde-date label="计划开始时间" dateType="date" v-model="info.date" />
        </div>
        <div class="two-col intro-y">
          <wde-switch label="是否激活" v-model="info.isActive" />
        </div> -->
      </template>
    </v-edit>
  </div>
</template>

<script setup>
import {ref, toRefs, watch, getCurrentInstance, onMounted} from "vue";
import {useEditLogic} from "@/views/base/useEditLogic";
import * as $api from "@/api/api.js";
import {ElMessage} from 'element-plus'
import {EquipmentInsert, EquipmentUpdate, MobileRegInsert, MobileRegUpdate, ReferDict} from "@/api/api.js";

const {proxy} = getCurrentInstance();
const emit = defineEmits(["close"]);

const props = defineProps({
  args: {
    type: Object,
    default: () => null,
  },
  init: {
    type: Object,
    default: () => null,
  },
});
// 使用 toRefs 解构 props
const {init} = toRefs(props);
const info = ref({
  regKey: "",
  imei: "",
  model: "",
  modelCode: "",
  mobilePerson: "",
  createrKey: "",
  createrName: "",
  createTime: "",
  stopFlag: false
});
const userKey = ref(localStorage.getItem("userKey"));
const repeatPwd = ref("");
// const info = ref({
//   tchKey: "",
//   tchNum: "",
//   tchName: "",
//   sex: true,
//   tel: "",
//   email: "",
//   title: "",
//   birthday: "",
//   nation: "",
//   idType: "",
//   idCode: "",
//   stopFlag: true,
//   remark: "",
// });
const equtStateList = ref([]);
const rules = ref({
  imei: [
    {required: true, message: "请输入设备编码", trigger: "blur"},
  ],
  mobilePerson: [
    {required: true, message: "请输入设备用户", trigger: "blur"},
  ],
  tel: [
    {required: true, message: "请输入手机号", trigger: "blur"},
  ],
});
onMounted(
    () => {
      getEquiStateList();
    }
)
const getEquiStateList = () => {
  $api.ReferDict({
    plantKey: "",
    userKey: userKey.value,
    key: "10"
  }).then((res) => {
    equtStateList.value = res.subs.map(item => {
      return {
        value: item.dictID,
        label: item.dictName,
      }
    });
  });
};
const buildAdd = () => {
  info.value.createTime = new Date();
  return $api.MobileRegInsert(info.value);
  // 构建新增逻辑
};

const buildUpdate = () => {
  return $api.MobileRegUpdate(info.value);
  // return $api.BATeacherUpdate(info.value);
};
const resetAdd = () => {
  proxy.$propsEmpty(info.value);
  // proxy.$propsCopy(info.value, props.init);
};
const resetUpdate = () => {
  proxy.$propsCopy(info.value, props.init);
};
const clearInfo = () => {
  proxy.$propsEmpty(info.value);
};

const close = () => {
  emit("close");
};
const {loading, comm, isView, reset, save, VEdit} = useEditLogic(
    init,
    info,
    resetAdd,
    resetUpdate,
    clearInfo,
    buildAdd,
    buildUpdate,
    close
);
watch(
    () => comm,
    (newVal) => {
      console.log(newVal);
    }
);
watch(
    () => props.init,
    (newVal) => {
      if (newVal) repeatPwd.value = newVal.userPwd;
    }
);
</script>

<style></style>